package models

import (
	"gitee.com/sansaniot/ssiot-core/httpmvc/model"
	"gorm.io/datatypes"

	"ssforward/common/dto"
	"ssforward/internal/forward/models/query"
)

type SysForwardRule struct {
	Id           string         `json:"id" gorm:"primaryKey;type:varchar(64);comment:id"`
	Name         string         `json:"name" gorm:"type:varchar(255);comment:转发规则名称"`
	ServerId     string         `json:"serverId" gorm:"type:varchar(64);comment:转发服务id"`
	ProductModel string         `json:"productModel" gorm:"type:varchar(255);comment:按产品型号的设备"`
	DevSn        string         `json:"devSn" gorm:"type:varchar(255);comment:按设备"`
	VariableMap  datatypes.JSON `json:"variableMap"  gorm:"type:jsonb;comment:变量映射json配置"`
	CreatorId    string         `json:"creatorId" gorm:"type:varchar(64);comment:创建人id"`
	AgencyId     string         `json:"agencyId" gorm:"type:varchar(64);comment:创建人机构用户id"`
	CreateTime   *dto.LocalTime `json:"createTime" gorm:"type:timestamptz(6);default:CURRENT_TIMESTAMP;comment:创建时间"`
	UpdateTime   *dto.LocalTime `json:"updateTime" gorm:"type:timestamptz(6);comment:更新时间"`
	ServerType   string         `json:"serverType" gorm:"type:varchar(10);comment:类型"`
	RuleScript   string         `json:"ruleScript" gorm:"type:text;comment:规则脚本"`
	model.Model

	ForwardServer *SysForwardServer `json:"server" gorm:"foreignKey:Id;references:ServerId;"`
}

func (SysForwardRule) TableName() string {
	return "sys_forward_rule"
}

func (e *SysForwardRule) Generate() interface{} {
	o := *e
	return &o
}

func (e *SysForwardRule) GetId() interface{} {
	return e.Id
}

func (e *SysForwardRule) FindRuleList(req *query.ForwardRuleQuery) (err error, result interface{}) {
	list := make([]SysForwardRule, 0)
	var count int64
	if req.AgencyId != req.UserId {
		// 非租户查无数据
		if req.PageIndex > 0 && req.PageSize > 0 {
			result = &dto.Page{
				Records:   list,
				Total:     count,
				PageSize:  req.GetPageSize(),
				PageIndex: req.GetPageIndex(),
				Pages:     (count + int64(req.GetPageSize()) - 1) / int64(req.GetPageSize()),
				Other:     nil,
			}
		} else {
			result = list
		}
		return
	}
	if req.PageIndex > 0 && req.PageSize > 0 {
		err = e.Orm.Preload("ForwardServer").Scopes(
			dto.MakeCondition(req.GetNeedSearch()),
			dto.OrderDest("create_time", true, req.OrderBy),
			dto.Paginate(req.GetPageSize(), req.GetPageIndex()),
		).Find(&list).Limit(-1).Offset(-1).Count(&count).Error
		result = &dto.Page{
			Records:   list,
			Total:     count,
			PageSize:  req.GetPageSize(),
			PageIndex: req.GetPageIndex(),
			Pages:     (count + int64(req.GetPageSize()) - 1) / int64(req.GetPageSize()),
			Other:     nil,
		}
	} else {
		err = e.Orm.Preload("ForwardServer").Scopes(
			dto.MakeCondition(req.GetNeedSearch()),
			dto.OrderDest("create_time", true, req.OrderBy),
		).Find(&list).Error
		result = list
	}
	return
}

func (e *SysForwardRule) FindServer(req *query.ForwardRuleQuery) (err error, one SysForwardRule) {
	err = e.Orm.Preload("ForwardServer").Scopes(dto.MakeCondition(req.GetNeedSearch())).First(&one).Error
	return
}
